<?php

class Instituciones_Model_DbTable_Normasies extends Zend_Db_Table_Abstract {
	
	protected $_name = 'normasies';
	protected $_primary = 'codnorma';
	protected $_rowClass = 'App_Audit_Row';

	public function getSelectFetchAll($codnormainterna,$codinstitucion,$tipo=1) {
//		    $orderby = array ('fechacrea DESC' );
//			$where = "codnormainterna='{$codnormainterna}' ";
//			return $this->select ( self::SELECT_WITH_FROM_PART )->where($where)->order ( $orderby )->query()->fetchAll();
	    $select = $this->select()
	                   ->from(array("ni"=>"normasies"))
	                   ->join(array("s"=>"solicitudes_cna"),"ni.codsolicitud=s.codsolicitud","")
	                   ->join(array("dn"=>"documentosnormas"),"ni.codnorma=dn.codnorma","")
	                   ->join(array("d"=>"documentos_cna"),"dn.coddocumento=d.coddocumento","*")
	                   ->where ("ni.codinstitucion='{$codinstitucion}'")
	                   ->where ("s.tiposolicitud='5'")
	                   ->where ("s.actual='S'")
	                   ->where("ni.codnormainterna=?",$codnormainterna)
	                   ->where("dn.tipodocumento=?",$tipo)
	                   ->order("ni.fechanorma DESC");
	    $select->setIntegrityCheck(false);
//	    echo $sql = $select->__toString();
		return $select->query()->fetchAll();
	}

	public function cargarUltimaNorma($codnormainterna,$codinstitucion) {
	    $select = $this->select()
	                   ->from (array("ni"=>"normasies"))
	                   ->join(array("s"=>"solicitudes_cna"),"ni.codsolicitud=s.codsolicitud","")
	                   ->where ("s.tiposolicitud='5'")
	                   ->where ("s.actual='S'")
	                   ->where ("ni.codnormainterna='{$codnormainterna}'")
	                   ->where ("ni.codinstitucion='{$codinstitucion}'")
	                   ->order ('ni.fechacrea DESC')
	                   ->limit ( 1 );
	     //       echo $select->__toString();
	    return $select->query()->fetch();
	    
		//$orderby = array ('fechacrea DESC' );
		//$where = "codnormainterna='{$codnormainterna}'";
		//return $this->select ( self::SELECT_WITH_FROM_PART )->where($where)->order ( $orderby )->limit(1)->query()->fetch();
	}
	
	public function getcodigoAdjunto($codnorma,$tipo=1) {
		return $this->getAdapter()->fetchOne("SELECT coddocumento FROM documentosnormas WHERE codnorma={$codnorma} AND tipodocumento={$tipo}" );
	}
	
	public function getSelectAdjuntos($codnorma,$tipo=1) {
	    $select = $this->select()
	                   ->from(array("d"=>"documentos_cna"))
	                   ->join(array("dn"=>"documentosnormas"),"dn.coddocumento=d.coddocumento")
	                   ->where("dn.codnorma=?",$codnorma)
	                   ->where("dn.tipodocumento=?",$tipo)
	                   ->order("d.coddocumento");
	    $select->setIntegrityCheck(false);
		return $select->query()->fetchAll();
	}
	
    public function actualizarCompletitud($codnorma) {
		$select = $this->getAdapter()->query("SELECT * FROM actualizar_completitud_ies({$codnorma})");
	}
	
    public function ajustarCompletitud($codinstitucion,$normainterna,$completo="S") {
        $select = $this->getAdapter()->query("INSERT INTO completitud_ies_cna (codinstitucion, codnormainterna, completo) VALUES ({$codinstitucion}, '{$normainterna}', '{$completo}')");
	}
	
}//fin de la clase

